package com.gengao.springbootcase.controller;

import com.gengao.springbootcase.mapper.DeptMapper;
import com.gengao.springbootcase.pojo.Result;
import com.gengao.springbootcase.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import com.gengao.springbootcase.pojo.Dept;


import java.time.LocalDate;
import java.util.List;


@RestController
//@Scope("prototype") //设置bean作用域为多例
public class DeptController {
    @Autowired
    private DeptService deptService;
    private static Logger log= LoggerFactory.getLogger(DeptController.class);
//  查询部门
    @RequestMapping(value = "/depts",method = RequestMethod.GET)//指定请求方式为get
    public Result list(){
        log.info("查询全部部门数据");
        List<Dept> deptList=deptService.list();
        return Result.success(deptList);
    }


    @RequestMapping(value = "depts/{id}",method = RequestMethod.GET)
    public Result getDeptById(@PathVariable Integer id){
        Dept dept=deptService.getById(id);
        return Result.success(dept);
    }

//    删除部门
    @RequestMapping(value = "/depts/{id}",method = RequestMethod.DELETE)
    public Result deleteDept(@PathVariable Integer id) throws Exception{
        log.info("根据id删除部门:{}",id);
        deptService.delete(id);
        return Result.success();
    }

//    新增部门   如何接收json数据 @RequestBody
    @RequestMapping(value = "depts",method = RequestMethod.POST)
    public Result insertDept(@RequestBody Dept dept){

        log.info("添加部门:{}",dept);
        deptService.insert(dept);
        return Result.success();
    }
    @RequestMapping(value = "/depts",method = RequestMethod.PUT)
    public Result update(@RequestBody Dept dept){
        log.info("修改部门信息根据id");
        deptService.update(dept);
        System.out.println(dept.getId());
        System.out.println(dept.getName());

        return Result.success();

    }
}
